from django.contrib.auth.models import User, Group, Permission
from rest_framework import serializers

from master.models import Depts



from master.models import Depts

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')

class GroupSerializer(serializers.HyperlinkedModelSerializer):
    permissions = serializers.ManySlugRelatedField(
        slug_field='codename',
        queryset=Permission.objects.all()
    )

    class Meta:
        model = Group
        fields = ('url', 'name', 'permissions')


class FooSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')


'''
class DeptsSerializer(serializers.ModelSerializer):
    
    class Meta:
        model = Depts
        fields = ('id', 'code', 'name')
'''        
        
        
        
        
class DeptsSerializer(serializers.Serializer):
    pk = serializers.Field()  # Note: `Field` is an untyped read-only field.
    
    code = serializers.CharField(required=False,
                                  max_length=100)
    name = serializers.CharField(
                                 max_length=100000)
    
    
    foo = "var"
    
    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance.
        """
        if instance:
            # Update existing instance
            instance.code = attrs['code']
            instance.name = attrs['name']
            instance.foo = "var"
            
            return instance

        # Create new instance
        return Depts(**attrs)